package com.woniuxy.lrz.carlproject.service.impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.model.Manager;
import com.woniuxy.lrz.carlproject.dao.ManagerMapper;
import com.woniuxy.lrz.carlproject.service.ManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Set;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    @Resource
    private ManagerMapper managerMapper;
    @Resource
    private ManagerService managerService;
    @Resource
    private RedisTemplate<String,String> rts;

    @Override
    public String managerLogin(String managerName, String managerPwd) {
        //加密managerPwd
        String encrpt = DigestUtil.sha256Hex(managerPwd).toString();
        //验证用户名是否存在
        Manager manager_name = managerMapper.selectOne(new QueryWrapper<Manager>().eq("manager_name", managerName));
        if (manager_name == null) {
            throw new AppException("登录失败，用户名不存在");
        }
        //验证密码是否正确

        Manager manager = managerMapper.selectOne(new QueryWrapper<Manager>().eq("manager_name", managerName)
                .eq("manager_pwd", encrpt));
        if (manager == null) {
            throw new AppException("登录失败，密码错误");
        }
        StpUtil.login(manager.getManagerId(), SaLoginConfig.setExtra("managerName", managerName)
                .setExtra("SB", "BS"));
        //返回Token
        return StpUtil.getTokenValue();

    }

    @Override
    public String phoneLogin(String phone, String code) {
        //验证手机号是否正确
        Manager phone1 = managerService.getOne(new QueryWrapper<Manager>().eq("phone", phone));
        if(phone1==null){
            throw new AppException("手机号输入有误，请重新输入");
        }

        //验证验证码是否过期
        Long expire = rts.getExpire("car:phoneLogin:"+phone);

        if(expire<0)throw new AppException("验证码已过期");

        Set<String> range = rts.opsForZSet().rangeByScore("car:phoneLogin:"+phone,0,4);

        //验证是否输入错误次数已超
        if(range.size()==0||range.size()<0)throw new AppException("输入次数已超5次，请稍后再试！");
        ArrayList<String> list=new ArrayList<>();
        list.addAll(range);
        String s = list.get(0);
        //验证验证码是否正确，正确则返回，不正确则错误次数加1
        if(!s.equals(code)){
            rts.opsForZSet().incrementScore("car:phoneLogin:"+phone,s,1);
            Double score = rts.opsForZSet().score("car:phoneLogin:"+phone, s);
            int i = Integer.parseInt((score+"").substring(0,1));
            if(i==5){
                throw new AppException("输入次数已到5次，请稍后再试！");
            }
            throw new AppException("输入错误！还剩"+(5-i)+"次机会");
        }
        Manager phones = managerMapper.selectOne(new QueryWrapper<Manager>().eq("phone", phone));
        StpUtil.login(phones.getManagerId(), SaLoginConfig.setExtra("managerName", phones.getManagerName())
                .setExtra("SB", "BS"));
        return StpUtil.getTokenValue();
    }
}
